package com.config.dao;

import java.util.HashMap;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.base.dao.TemplateDaoImpl;
import com.base.utils.BeanToSqlutils;
import com.config.entity.SystemParams;

@Repository("systemParamsDao")
public class SystemParamsDaoImpl extends TemplateDaoImpl<SystemParams, Integer> implements
		SystemParamsDao {
	
	/**
	 * 根据系统变量名称查询系统变量
	 * @param key
	 * @return
	 */
	public SystemParams getByKey(String key){
		String sql = BeanToSqlutils.getSelectSql("p", SystemParams.class)
				+ " where " + BeanToSqlutils.equalCondition("key", "p");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("key", key);
		return jt.queryForObject(sql, params,
				ParameterizedBeanPropertyRowMapper
						.newInstance(SystemParams.class));
	}
	
	/**
	 * 新增或修改系统变量，有责修改，无责新增
	 * @param param
	 */
	public void saveOrUpdate(SystemParams param){
		SystemParams p=this.getByKey(param.getKey());
		if(p==null){
			this.save(param);
		}else{
			p.setValue(param.getValue());
			this.update(p);
		}
	}
}
